package com.xudow.app.dynamicstate_old.module.user;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.activeshare.edu.ucenter.models.base.Courses;
import com.activeshare.edu.ucenter.models.course.CourseWithOtherInfo;
import com.jakewharton.rxbinding.view.RxView;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.beam.expansion.list.ListConfig;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.tagview.TAGView;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.domain.entity.UserCourseWithOtherInfo;
import com.xudow.app.dynamicstate_old.module.user.viewholder.PreferredCourseViewHolder;
import com.xudow.app.ui.CourseDetailActivity;

/**
 * Created by ske on 2016/6/12.
 */
@RequiresPresenter(UserCoursePresenter.class)
public class UserCourseFragment extends BeamListFragment<UserCoursePresenter, UserCourseWithOtherInfo> {

    public static UserCourseFragment newInstance(long userId, String introduce, String data) {
        Bundle args = new Bundle();
        args.putLong("userId", userId);
        args.putString("introduce", introduce);
        args.putString("data", data);
        UserCourseFragment fragment = new UserCourseFragment();
        fragment.setArguments(args);
        return fragment;
    }

    long userProfileId;
    String introduce;
    String data;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        introduce = getArguments().getString("introduce");
        data = getArguments().getString("data");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        userProfileId = getArguments().getLong("userId");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getPresenter().getAdapter().setOnItemClickListener(position -> {
            Intent intent = new Intent(getActivity(), CourseDetailActivity.class);
            intent.putExtra("courseId", getPresenter().getAdapter().getItem(position).getId());
            startActivity(intent);
        });
    }

    private View getEmptyView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_no_course, null, false);
        if (userProfileId == XApplication.getInstance().getUserProfileId()) {
            if (!TextUtils.isEmpty(introduce)) {
                ((TAGView) view.findViewById(R.id.tag_apply)).setBackgroundColor(getResources().getColor(R.color.gray200));
            } else
                RxView.clicks(view.findViewById(R.id.tag_apply))
                        .subscribe(v -> {
                            Intent intent = new Intent(getActivity(), WriteIntroduceActivity.class);
                            intent.putExtra("data", data);
                            startActivity(intent);
                        });
        } else
            view.findViewById(R.id.tag_apply).setVisibility(View.GONE);
        return view;
    }

    @Override
    public ListConfig getConfig() {
        return super.getConfig()
                .setRefreshAble(false)
                .setContainerEmptyView(getEmptyView())
                .setContainerErrorView(getEmptyView());
    }

    @Override
    public BaseViewHolder<UserCourseWithOtherInfo> getViewHolder(ViewGroup parent, int viewType) {
        return new PreferredCourseViewHolder(parent);
    }
}
